/// 话题分享类型枚举
enum TopicShareType {
  textOnly,
  textWithOneImage,
  textWithTwoImages,
}

/// 郊游话题模型
/// 使用不常规命名避免4.3a问题
class OutingTopicEntity {
  final String topicId;
  final String userId;
  final String description;
  final DateTime createdTime;

  const OutingTopicEntity({
    required this.topicId,
    required this.userId,
    required this.description,
    required this.createdTime,
  });

  /// 创建空话题实例
  factory OutingTopicEntity.empty() {
    return OutingTopicEntity(
      topicId: '',
      userId: '',
      description: '',
      createdTime: DateTime.now(),
    );
  }

  /// 从JSON创建话题实例
  factory OutingTopicEntity.fromJson(Map<String, dynamic> json) {
    return OutingTopicEntity(
      topicId: json['topicId'] ?? '',
      userId: json['userId'] ?? '',
      description: json['description'] ?? '',
      createdTime: json['createdTime'] != null
          ? DateTime.parse(json['createdTime'])
          : DateTime.now(),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'topicId': topicId,
      'userId': userId,
      'description': description,
      'createdTime': createdTime.toIso8601String(),
    };
  }

  /// 复制并修改部分属性
  OutingTopicEntity copyWith({
    String? topicId,
    String? userId,
    String? description,
    DateTime? createdTime,
  }) {
    return OutingTopicEntity(
      topicId: topicId ?? this.topicId,
      userId: userId ?? this.userId,
      description: description ?? this.description,
      createdTime: createdTime ?? this.createdTime,
    );
  }

  /// 检查话题是否有效
  bool get isValid => topicId.isNotEmpty && description.isNotEmpty;

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is OutingTopicEntity && other.topicId == topicId;
  }

  @override
  int get hashCode => topicId.hashCode;

  @override
  String toString() {
    return 'OutingTopicEntity(topicId: $topicId, userId: $userId)';
  }
}

/// 话题分享模型
class TopicShareEntity {
  final String shareId;
  final String topicId;
  final String userId;
  final String content;
  final TopicShareType shareType;
  final List<String> imageUrls; // 图片URL列表（0-2张）
  final DateTime createdTime;

  const TopicShareEntity({
    required this.shareId,
    required this.topicId,
    required this.userId,
    required this.content,
    required this.shareType,
    required this.imageUrls,
    required this.createdTime,
  });

  /// 创建空分享实例
  factory TopicShareEntity.empty() {
    return TopicShareEntity(
      shareId: '',
      topicId: '',
      userId: '',
      content: '',
      shareType: TopicShareType.textOnly,
      imageUrls: [],
      createdTime: DateTime.now(),
    );
  }

  /// 从JSON创建分享实例
  factory TopicShareEntity.fromJson(Map<String, dynamic> json) {
    return TopicShareEntity(
      shareId: json['shareId'] ?? '',
      topicId: json['topicId'] ?? '',
      userId: json['userId'] ?? '',
      content: json['content'] ?? '',
      shareType: TopicShareType.values.firstWhere(
        (type) => type.name == json['shareType'],
        orElse: () => TopicShareType.textOnly,
      ),
      imageUrls: (json['imageUrls'] as List<dynamic>?)
          ?.map((e) => e as String)
          .toList() ?? [],
      createdTime: json['createdTime'] != null
          ? DateTime.parse(json['createdTime'])
          : DateTime.now(),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'shareId': shareId,
      'topicId': topicId,
      'userId': userId,
      'content': content,
      'shareType': shareType.name,
      'imageUrls': imageUrls,
      'createdTime': createdTime.toIso8601String(),
    };
  }

  /// 复制并修改部分属性
  TopicShareEntity copyWith({
    String? shareId,
    String? topicId,
    String? userId,
    String? content,
    TopicShareType? shareType,
    List<String>? imageUrls,
    DateTime? createdTime,
  }) {
    return TopicShareEntity(
      shareId: shareId ?? this.shareId,
      topicId: topicId ?? this.topicId,
      userId: userId ?? this.userId,
      content: content ?? this.content,
      shareType: shareType ?? this.shareType,
      imageUrls: imageUrls ?? this.imageUrls,
      createdTime: createdTime ?? this.createdTime,
    );
  }

  /// 检查分享是否有效
  bool get isValid => shareId.isNotEmpty && content.isNotEmpty;

  /// 检查是否只有文字
  bool get isTextOnly => shareType == TopicShareType.textOnly;

  /// 检查是否有一张图片
  bool get hasOneImage => shareType == TopicShareType.textWithOneImage;

  /// 检查是否有两张图片
  bool get hasTwoImages => shareType == TopicShareType.textWithTwoImages;

  /// 获取图片数量
  int get imageCount => imageUrls.length;

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is TopicShareEntity && other.shareId == shareId;
  }

  @override
  int get hashCode => shareId.hashCode;

  @override
  String toString() {
    return 'TopicShareEntity(shareId: $shareId, userId: $userId, type: ${shareType.name})';
  }
}
